package com.clinic.controller;

import com.clinic.annoation.MyLogAnnoation;
import com.clinic.entity.Charge;
import com.clinic.service.ChargeService;
import com.clinic.vo.ActionResult;
import com.clinic.vo.ChargeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Charge)表控制层
 *收费表/项目中的收费管理
 * @author makejava
 * @since 2020-11-12 15:55:10
 */
@RequestMapping(value = "/api")
@RestController
public class ChargeController {
    /**
     * 服务对象
     */
    @Autowired
    @Resource
    private ChargeService chargeService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
//    @GetMapping("selectOne")
//    public Charge selectOne(Integer id) {
//        return this.chargeService.queryById(id);
//    }

    //开始
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody()
    public ActionResult findAll() {
        List<Charge> all = chargeService.findAll();
        for (Charge charge : all) {
//            System.out.println("打印拿到的数据："+charge);
            System.out.println(charge);
        }
        //进行判断
        if (all != null) {
            ActionResult actionResult = new ActionResult(200, "查询成功", all);
            return actionResult;
        } else {
            ActionResult actionResult = new ActionResult(403, "查询失败", null);
            return actionResult;
        }
    }

    //根据id来删除
    @MyLogAnnoation
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ResponseBody()
    public ActionResult delete(int id) {
        int count = chargeService.delete(id);
        System.out.println("删除的过程中" + count + "行受到了影响");
        if (count > -1) {
            ActionResult actionResult = new ActionResult(200, "删除成功", null);
            return actionResult;
        } else {
            ActionResult actionResult = new ActionResult(403, "删除失败", null);
            return actionResult;
        }
    }

    @RequestMapping(value = "/allcharge", method = RequestMethod.GET)
    @ResponseBody()
    public ActionResult findAllCharge(int pid) {
//        从页面拿到cid,根据吃的来拿到pid
//        Charge charge = chargeService.selectById(cid);
//        Integer pid = charge.getPid();
//        System.out.println("拿到的pid是："+pid);

        List<ChargeVo> allCharge = chargeService.findAllCharge(pid);
        System.out.println("拿到的集合是：" + allCharge);
        for (ChargeVo chargeVo : allCharge) {
            System.out.println("待收费下的收费：" + allCharge);
        }
        if (allCharge != null) {
            ActionResult actionResult = new ActionResult(200, "查询成功", allCharge);
            return actionResult;
        } else {
            ActionResult actionResult = new ActionResult(403, "查询失败", "");
            return actionResult;
        }
    }

    @MyLogAnnoation
    @RequestMapping(value = "/updata", method = RequestMethod.PUT)
    @ResponseBody
    public ActionResult updata(@RequestBody Charge charge) {
        int count = chargeService.updateByCid(charge);
        System.out.println("传过来的参数是：" + charge);
        if (count > 0) {
            ActionResult actionResult = new ActionResult(200, "收费成功", null);
            return actionResult;
        } else {
            ActionResult actionResult = new ActionResult(403, "收费失败", null);
            return actionResult;
        }
    }

}